<?php

class fI18N {
	/**
	 * Culture
	 *
	 * @var string
	 */
	private $_culture;
	
	/**
	 * Language Source
	 *
	 * @var fLanguageSource
	 */
	private $_languageSource;
	
	public function __construct() {		
		$this->_culture = fFactory::getRouter()->getCultureInfo();			
	}
	
	/**
	 * Get LanguageSource
	 *
	 * @return fLanguageSourceIni
	 */
	public function getLanguageSource() {
		if ($this->_languageSource == null) {
			$type = fConfig::get('source', 'i18n');
			if ($type == null) {
				$type = 'array';
			}			
			$class = 'fLanguageSource' . ucfirst($type);
            require_once _SYSTEMS_PATH_.'i18n'._DS_.$class.'.class.php';
			$this->_languageSource = new $class($this->_culture);							
		}
		
		return $this->_languageSource;
	}
	
	/**
	 * Get culture
	 *
	 * @return string
	 */
	public function getCulture() {
		return $this->_culture;		
	}
	
	/**
	 * Sets the current culture for i18n format objects.
	 *
	 * @param string $culture The culture
	 */
	public function setCulture($culture) {
		$this->_culture = $culture;		
		// change user locale for formatting, collation, and internal error messages
		setlocale(LC_ALL, $culture.'.utf8', $culture.'.UTF8', $culture.'.utf-8', $culture.'.UTF-8');
		setlocale(LC_COLLATE, $culture.'.utf8', $culture.'.UTF8', $culture.'.utf-8', $culture.'.UTF-8');
		setlocale(LC_CTYPE, $culture.'.utf8', $culture.'.UTF8', $culture.'.utf-8', $culture.'.UTF-8');
		setlocale(LC_MONETARY, $culture.'.utf8', $culture.'.UTF8', $culture.'.utf-8', $culture.'.UTF-8');
		setlocale(LC_TIME, $culture.'.utf8', $culture.'.UTF8', $culture.'.utf-8', $culture.'.UTF-8');
		
		if ($this->_languageSource != null) {
			$this->_languageSource->setCulture($culture);
		}
	}
	
	/**
	 * Gets the translation for the given string 
	 *
	 * @param string $singular
	 * @return string
	 */
	public function __($singular) {
        return $this->_($singular);
	}

    public function _($singular) {
        return $this->getLanguageSource()->translate($singular);
    }
}
/*
function _($singular) {
	return fFactory::getI18N()->_($singular);
}*/